package com.zeus.controller;

import com.zeus.mapper.CartMapper;
import com.zeus.mapper.OrderDetailMapper;
import com.zeus.pojo.*;
import com.zeus.service.AddressService;
import com.zeus.service.CartService;
import com.zeus.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 冯扬龙
 * @date 2021/4/20 14:58
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;

//    前端传skuid过来
    @RequestMapping("submitOrder")
    public ModelAndView submitOrder(String skuIdStr, HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("loginUser") != null) {
            //1.查询该用户的地址信息
            Users user = (Users) session.getAttribute("loginUser");
            System.out.println(user.getUserId());
            List<Address> addressList = addressService.submitOrder(user.getUserId());
            //2.查询准备购买的商品信息
            Cart cart = cartService.showCartList(user.getCart().getCartId());
            //4001,4009  ==> [4001] [4009]
            String[] skuIdArr = skuIdStr.split(",");
            //存放所有选中的商品的信息
            List<CartItem> selectCartItemList = new ArrayList<CartItem>();
            float totalPrice = 0;
            for (CartItem cartItem : cart.getCartItemList()) {
                if (Arrays.asList(skuIdArr).contains(cartItem.getSkuId() + "")) {
                    selectCartItemList.add(cartItem);
                    totalPrice += cartItem.getPrice() * cartItem.getBuyNum();
                }
            }
            //3.传递到视图页展示
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("addressList", addressList);
            modelAndView.addObject("selectCartItemList", selectCartItemList);
            modelAndView.addObject("totalPrice", totalPrice);
            modelAndView.addObject("skuIdStr", skuIdStr);
            modelAndView.setViewName("pay");
            return modelAndView;

        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }

//    点击结算和提交订单，产生的一系列操作
    @RequestMapping("/addOrder")
    @ResponseBody
    public int addOrder(int addressId, String skuIdStr, float orderAmount, HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session.getAttribute("loginUser") != null){
            Users users = (Users)session.getAttribute("loginUser");
//            将接收到的信息组装为order对象
            Order order = new Order();
            order.setAddressId(addressId);
            order.setOrderAmount(orderAmount);
            order.setUserId(users.getUserId());
            String[] skuIdArr = skuIdStr.split(",");
            List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
            for (String skuId : skuIdArr){
                OrderDetail orderDetail = new OrderDetail();
//                "4002"->4002
                 orderDetail.setSkuId(Integer.parseInt(skuId));
                 orderDetailList.add(orderDetail);
            }
            order.setOrderDetailList(orderDetailList);
//            返回客户端
            int orderId = orderService.addOrder(order,users.getCart().getCartId());
//            更新购物车里面的物品，更新session信息，同步
            users.getCart().setTotalNum(users.getCart().getTotalNum()- skuIdArr.length);
            session.setAttribute("loginUser",users);
            return orderId;
        }else {
            return -1;
        }
    }

//    进入订单管理
    @RequestMapping("/orderList")
    public ModelAndView orderList(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session.getAttribute("loginUser") != null){
            Users users = (Users)session.getAttribute("loginUser");
            List<Order> orderList = orderService.selectOrderList(users.getUserId());
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("orderList",orderList);
            modelAndView.setViewName("person/order");
            return modelAndView;
        }else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;
        }

    }
}
